package org.monster.boot;

import org.monster.app.DosReplace;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * {@link SocketReceiver}的代理类，将写入Socket的信息替换成正确Header信息
 *
 * @author Monster
 */
public class SocketReceiverProxy implements Receiver, Proxy {

    /**
     * 日志器
     */
    private static final Logger logger = Logger.getLogger(SocketReceiverProxy.class
            .getName());

    /**
     * 套接字接收器
     */
    private SocketReceiver receiver;

    /**
     * Dos替换
     */
    private DosReplace replace = new DosReplace();

    /**
     * 带参的构造方法({@link SocketReceiver},{@link DosReplace})
     *
     * @param receiver {@link SocketReceiver}
     */
    public SocketReceiverProxy(SocketReceiver receiver) {
        this.receiver = receiver;
    }

    /**
     * 接收
     *
     * @param dosCommand {@link String} dos命令
     */
    @Override
    public void receive(String dosCommand) {
        dosCommand = before(dosCommand);
        try {
            receiver.writeSocket(dosCommand);
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    /**
     * 之前
     * 在之前替换正确Header信息
     *
     * @param dosCommand dos命令
     * @return
     */
    public String before(String dosCommand) {
        return replace.replace(dosCommand);
    }

    /**
     * 之后
     *
     * @param dosCommand dos命令
     * @return dos命令
     */
    @Override
    public String after(String dosCommand) {
        return null;
    }
}
